#!/usr/bin/perl

use File::Copy;

my $catalog = "catalog.rdf";

# Break the catalog up into individual files.  
# Then we can query/process using other languages
# into the appropriate formats.

open CATALOG, "<$catalog" || die "can't open $catalog: $!";

my $rdf_dir = "gutenberg_scifi/rdfs";
my $seen_zip = 0;
my $seen_english = 0;
my $seen_html = 0;
my $seen_txt = 0;
my $seen_pdf = 0;
my $paragraph = "";

my $book_id;

unlink(glob("$rdf_dir/*.rdf"));

while (<CATALOG>) {
    if ($_ =~ /etext([0-9]+)"/) {
	$book_id = $1;
	print "looking at book $book_id\n";
    }

    if ( $_ =~ /<dcterms:IMT><rdf:value>application\/zip/ ) {
	$seen_zip = 1;
    }
    if ( $_ =~ /<dc:format><dcterms:IMT><rdf:value>text\/html/ ) {
	$seen_html = 1;
    }
    if ( $_ =~ /<dc:format><dcterms:IMT><rdf:value>text\/plain/ ) {
	$seen_txt = 1;
    }
    if ( $_ =~ /<dc:format><dcterms:IMT><rdf:value>application\/pdf/ ) {
	$seen_pdf = 1;
    }

    $paragraph .= $_;

    if ($_ =~ /<\/pgterms:etext>/ ) {
	# Dump the segment
	if (! -f "$rdf_dir/$book_id.rdf") {
	    copy("templates/book_header.rdf", "$rdf_dir/$book_id.rdf");
	}

	open RDF, ">>$rdf_dir/$book_id.rdf";
	print RDF $paragraph;
	close RDF;

	$paragraph = "";
	$seen_zip = 0;
	$seen_html = 0;
	$seen_txt = 0;
	$seen_book = 0;
	$seen_pdf = 0;
    }

    if ($_ =~ /<\/pgterms:file>/ ) {
	if ($seen_pdf || ($seen_zip && ($seen_html || $seen_txt))) {
	    # Dump the segment
	    open RDF, ">>$rdf_dir/$book_id.rdf";
	    print RDF $paragraph;
	    close RDF;
	}

	$paragraph = "";
	$seen_zip = 0;
	$seen_english = 0;
	$seen_html = 0;
	$seen_txt = 0;
	$seen_book = 0;
	$seen_pdf = 0;
    }
}

opendir(DIR, $rdf_dir) || die "can't opendir $rdf_dir: $!";
my @files = grep { /\.rdf$/ && -f "$rdf_dir/$_" } readdir(DIR);
closedir DIR;

foreach my $file (@files) {
    print "Appending to file $rdf_dir/$file\n";
    open RDF, ">>$rdf_dir/$file";
    print RDF "</rdf:RDF>\n";
    close RDF;
}
